package de.wiroo.setup.web.common;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

public abstract class ListViewPanel<T> extends Panel {
	/***/
	private static final long serialVersionUID = 1L;

	public ListViewPanel(String id) {
		super(id);
		init();
	}

	public ListViewPanel(String id, IModel<T> model) {
		super(id, model);
		init();
		
	}
	
	private void init() {
		add(new ListView<T>("list") {

			/***/
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<T> item) {
				Component itemComponent = createItemComponent("item");
				itemComponent.setDefaultModel(item.getModel());
				item.add(itemComponent);
			}

		});
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		onModelChanged();
	}

	@Override
	protected void onModelChanged() {
		super.onModelChanged();
		get("list").setDefaultModel(getDefaultModel());
	}

	protected abstract Component createItemComponent(String id);
}
